//
//  SongsMenuCollectionReusableView.m
//  NewMusic
//
//  Created by chuxinyu123 on 15/11/16.
//  Copyright © 2015年 Going. All rights reserved.
//

#import "SongsMenuCollectionReusableView.h"

@interface SongsMenuCollectionReusableView ()
@property (nonatomic, retain) UIView *blackView;
@property (nonatomic, retain) UIButton *buttonAll;

@end
@implementation SongsMenuCollectionReusableView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveNotificationOne:) name:@"SongsMenu" object:nil];
        self.blackView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 375, 40)];
        self.blackView.backgroundColor = [UIColor clearColor];
        [self buildButtonForAll];
        [self buildButtonForChoose];
        [self addSubview:_blackView];
        [self.blackView release];
    }
    return self;
}

- (void)buildButtonForChoose
{
    UIView *greenView = [[UIView alloc] init];
    greenView.frame = CGRectMake(240 + 60 - 0.5, 11, 1, 17);
    greenView.backgroundColor = [UIColor colorWithRed:180 / 255.0 green:180 / 255.0 blue:180 / 255.0 alpha:1.0];
    
    
    NSArray *array = @[@"推荐", @"最新"];
    for (NSInteger i = 0; i < 2; i++) {
        UIButton *chooseButton = [UIButton buttonWithType:UIButtonTypeCustom];
        chooseButton.frame = CGRectMake(i * 60 + 240, 7.5, 60, 25);
        chooseButton.titleLabel.textAlignment = NSTextAlignmentCenter;
        chooseButton.tag = 1010 + i;
        [chooseButton setTitle:array[i] forState:UIControlStateNormal];
        [chooseButton addTarget:self action:@selector(CXYChooseAction:) forControlEvents:UIControlEventTouchUpInside];
        chooseButton.titleLabel.font = [UIFont systemFontOfSize:15];
        if (i == 1) {
            [chooseButton setTitleColor:[UIColor colorWithRed:180 / 255.0 green:180 / 255.0 blue:180 / 255.0 alpha:1.0] forState:UIControlStateNormal];
        }else if (i == 0) {
            [chooseButton setTitleColor:[UIColor colorWithRed:38 / 255.0 green:185 / 255.0 blue:100 / 255.0 alpha:1.0] forState:UIControlStateNormal];
        }
        
        [_blackView addSubview:chooseButton];
    }
    [_blackView addSubview:greenView];
    [greenView release];
    
}

- (void)buildButtonForAll
{
    
    self.buttonAll = [UIButton buttonWithType:UIButtonTypeSystem];
    self.buttonAll.frame = CGRectMake(15, 7.5, 70, 25);
    self.buttonAll.backgroundColor = [UIColor clearColor];
    [self.buttonAll setTitleColor:[UIColor colorWithRed:38 / 255.0 green:185 / 255.0 blue:100 / 255.0 alpha:1.0] forState:UIControlStateNormal];
    self.buttonAll.titleLabel.font = [UIFont systemFontOfSize:15];
    self.buttonAll.layer.masksToBounds = YES;
    self.buttonAll.layer.cornerRadius = 5;
    //边框宽度
    self.buttonAll.layer.borderWidth = 1;
    //边框颜色
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    // 新建一个绿的ColorRef，用于设置边框（四个数字分别是 r, g, b, alpha）
    CGColorRef borderColorRef = CGColorCreate(colorSpace,(CGFloat[]){ 38 / 255.0, 185 / 255.0, 100 / 255.0, 1 });
    // 设置边框颜色
    self.buttonAll.layer.borderColor = borderColorRef;
    
    [self.buttonAll setTitle:@"全部" forState:UIControlStateNormal];
    [self.buttonAll addTarget:self action:@selector(CXYPresetAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [_blackView addSubview:_buttonAll];
}

//展示全部按键点击事件
- (void)CXYPresetAction:(UIButton *)sender
{
    [self.delegate CXYButton:sender];
}

- (void)CXYChooseAction:(UIButton *)sender
{
    if (sender.tag == 1011) {
        [[self.blackView viewWithTag:1010] setTitleColor:[UIColor colorWithRed:180 / 255.0 green:180 / 255.0 blue:180 / 255.0 alpha:1.0] forState:UIControlStateNormal];
        [sender setTitleColor:[UIColor colorWithRed:38 / 255.0 green:185 / 255.0 blue:100 / 255.0 alpha:1.0] forState:UIControlStateNormal];
        [self.delegate custumerButton:sender];
        
    }else if (sender.tag == 1010) {
        [[self.blackView viewWithTag:1011] setTitleColor:[UIColor colorWithRed:180 / 255.0 green:180 / 255.0 blue:180 / 255.0 alpha:1.0] forState:UIControlStateNormal];
        [sender setTitleColor:[UIColor colorWithRed:38 / 255.0 green:185 / 255.0 blue:100 / 255.0 alpha:1.0] forState:UIControlStateNormal];
        [self.delegate custumerButton:sender];
    }
}
- (void)receiveNotificationOne:(NSNotification *)notification
{
    [self.buttonAll setTitle:notification.userInfo[@"SongsTitle"] forState:UIControlStateNormal];
}

@end
